# function Determinant(A) # Returns the determinant of the matrix A.
# function Inverse(A) # Returns the inverse of a matrix A.
# function Identity(N) # returns NxN identity matrix.
#
#
# This text file explains and gives examples of
# some of the routines in the file All Library Routines, which
# should be Opened before trying any of these examples.
#
#################
#########
# The xCOD'sare
xDETERMINANT
# xCO2 xDETERMINANT(N:extended; theMatrix:RealArray; theDet, Err:Num) ; finds the determinant of a real matrix theMatrix[1…N,1…N] (which is destroyed). N must be < 1000. Err: 0=> success, -1=> N>1000.; an external program.
xINVERSE
# xCO2 xINVERSE(N:extended; InputMatrix,OutputMatrix:RealArray; Err:num) ;inverts a real matrix InputMatrix[1…N,1…N] (which is destroyed). N must be <1000. Err: 0=> success, -1=> N>1000, -2=> singular matrix ; an external program.
#######################
# The interface routines.
function Determinant(A) # Returns the determinant of the matrix A.
. var d,n, ans, temp, err
. # Input:
. # A[1…n,1…n] = square, real matrix
. # Ouput:
. # Determinant = real number
. begin
. d=dimension(a)
. if size(d)<>2 then
. print('•• ERROR : not a matrix.')
. else if d[1]<>d[2] then
. print('•• ERROR : not a square matrix.')
. else
. begin
. n = d[1];
. temp=a
. xDETERMINANT(n,temp,ans,err)
. if err<>0 then
. Determinant = "ERROR in xDETERMINANT, matrix must be smaller than 1000x1000"
. else
. Determinant = ans
. end
. end
.
function Inverse(A) # Returns the inverse of a matrix A.
. var d,n, ans, temp, err
. # Input:
. # a = square, real matrix
. # Output:
. # Inverse = square, real matrix such that Inverse • A = Identity.
. begin
. d=dimension(a)
. if size(d)<>2 then
. print('•• ERROR : not a matrix.')
. else if d[1]<>d[2] then
. print('•• ERROR : not a square matrix.')
. else
. begin
. n = d[1];
. temp=a ; ans=a # reserve storage space.
. xINVERSE(n,temp,ans,err)
. if err=0 then
. Inverse = ans
. else if err=-1 then
. Inverse = "ERROR in xINVERSE, matrix must be smaller than 1000x1000"
. else
. Inverse = "ERROR in xINVERSE, matrix is singular."
. end
. end
.
function Identity(N) # returns NxN identity matrix.
. var ans,range
. # Input: positive integer N
. # Output: Identity = B[1…N,1…N] with B[i,j] = 0 if i<>j; = 1 if i=j.
. begin
. range=1…N;
. ans[range,range] = 0; # This sets entire array to 0.
. for range do ans[range,range]=1; # While this sets diagonal to 1.
. Identity=ans
. end
.
###############################
# For example, if
a = [2,0,0; 1,1,-1; 0,1,0] # define an array "a".
a[1…3,1…3] = [[2, 0, 0],
. [1, 1, -1],
. [0, 1, 0]] .
# The determinant of "a" is
Determinant(a)
-2
# while the inverse of a is
aInv = Inverse(a)
aInv[1…3,1…3] = [[0.5, 0, 0],
. [0, 0, 1],
. [0.5, -1, 1]] .
# Using "•" (opt-8 on US keyboard) for matrix multiplication
# (or implicit multiplication (i.e. a space with no operation) with
# the Multiply Options (Other menu) set to • for 2-dim arrays)
# we get
a • aInv
[[1, 0, 0],
. [0, 1, 0],
. [0, 0, 1]] .
# which is the identity matrix.
########################
# The Identity function will create an identity matrix.